<template>
  <div class="f1 h0" v-show="active == 1">
    <el-tabs v-model="activeName" class="demo-tabs" @tab-change="tabChange">
      <el-tab-pane
        v-for="item in cates"
        :key="item.value"
        :label="item.description"
        :name="item.value"
      >
        <div v-if="item.sub_categories.length > 0" class="h100">
          <el-button @click="createPrompt" class="create-prompt"
            >新建prompt</el-button
          >
          <el-tabs @tab-change="tabChange2" v-model="childActiveName">
            <el-tab-pane
              v-for="child in item.sub_categories"
              :key="child.value"
              :label="child.description"
              :name="child.value"
              class="df fdc"
            >
              <el-table class="f1" :data="tableData">
                <el-table-column prop="id" label="PromptID"></el-table-column>
                <el-table-column
                  prop="prompt"
                  label="Prompt内容"
                ></el-table-column>
                <el-table-column
                  prop="updateDateStr"
                  label="创建时间"
                ></el-table-column>
                <el-table-column
                  prop="updateDateStr"
                  label="更新时间"
                ></el-table-column>
                <el-table-column label="状态">
                  <template #default="scope">
                    <p
                      :style="{
                        color: scope.row.status == 1 ? '#409EFF' : '#606266',
                      }"
                    >
                      {{ scope.row.statusText }}
                    </p>
                  </template>
                </el-table-column>
                <el-table-column prop="remark" label="备注"></el-table-column>
                <el-table-column label="操作">
                  <template #default="scope">
                    <div v-show="scope.row.status != 1">
                      <el-button @click="pushOnline(scope.row)" link
                        >推到线上</el-button
                      >
                      <el-button @click="deletePe(scope.row)" link
                        >删除</el-button
                      >
                    </div>
                    <el-button @click="editSpeedRead(scope.row)" link
                      >编辑</el-button
                    >
                  </template>
                </el-table-column>
              </el-table>
              <el-pagination
                v-model:current-page="currentPage"
                v-model:page-size="pageSize"
                :page-sizes="[10, 20, 30, 40]"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total"
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                style="height: 40px; margin-top: 20px"
              />
            </el-tab-pane>
          </el-tabs>
        </div>
        <div class="h100" v-else>
          <el-button @click="createPrompt" class="create-prompt"
            >新建prompt</el-button
          >
          <el-table :data="tableData">
            <el-table-column prop="id" label="PromptID"></el-table-column>
            <el-table-column prop="prompt" label="Prompt内容"></el-table-column>
            <el-table-column
              prop="updateDateStr"
              label="创建时间"
            ></el-table-column>
            <el-table-column
              prop="updateDateStr"
              label="更新时间"
            ></el-table-column>
            <el-table-column label="状态">
              <template #default="scope">
                <p
                  :style="{
                    color: scope.row.status == 1 ? '#409EFF' : '#606266',
                  }"
                >
                  {{ scope.row.statusText }}
                </p>
              </template>
            </el-table-column>
            <el-table-column prop="remark" label="备注"></el-table-column>
            <el-table-column label="操作">
              <template #default="scope">
                <div v-show="scope.row.status != 1">
                  <el-button @click="pushOnline(scope.row)" link
                    >推到线上</el-button
                  >

                  <el-button @click="deletePe(scope.row)" link>删除</el-button>
                </div>
                <el-button @click="editSpeedRead(scope.row)" link
                  >编辑</el-button
                >
              </template>
            </el-table-column>
          </el-table>
          <el-pagination
            v-model:current-page="currentPage"
            v-model:page-size="pageSize"
            :page-sizes="[10, 20, 30, 40]"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            style="height: 40px; margin-top: 20px"
          />
        </div>
      </el-tab-pane>
    </el-tabs>
    <el-drawer
      v-model="drawerVisable"
      title="I have a nested form inside!"
      direction="rtl"
      size="50%"
    >
    </el-drawer>
  </div>
  <div v-show="active == 2">
    <create-or-edit-prompt @backCreate="backCreate" />
  </div>
</template>

<script setup>
import { ref, onMounted, watch } from "vue";
import { peList, peStatus, delPe } from "@/apis/pe";
import createOrEditPrompt from "@/views/PE/createOrEditPrompt.vue";
import { pe_cate } from "@/apis/pe";
import { transDate } from "@/utils/transforDate";
import { useCategoryStore } from "@/store";

const store = useCategoryStore();

const activeName = ref("article_info");
const childActiveName = ref("summary");
const drawerVisable = ref(false);
const active = ref(1);
const cates = ref([]);
const tableData = ref([]);
const currentPage = ref(1);
const pageSize = ref(20);
const total = ref(0);

// const userData = ref({});
// const helloData = ref({});
// const messageData = ref({});
// const chatData = ref({});
// const experienceData = ref({});

onMounted(() => {
  get_pe_cate();
  get_pe_list();
});

const createPrompt = () => {
  active.value = 2;
  store.setCategroyMethod("create");
};
const pushOnline = (e) => {
  ElMessageBox.confirm("是否确定推到线上？", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
  })
    .then(async () => {
      let data = {};
      data.id = JSON.stringify(e.id);
      data.status = 1;
      try {
        let res = await peStatus(data);
        if (res.data.code == 200) {
          ElMessage.success("成功");
          getData();
        }
      } catch (e) {
        console.log(e, "eee");
      }
    })
    .catch(() => {
      ElMessage.info("取消");
    });
};

const editSpeedRead = (e) => {
  active.value = 2;
  store.setCategroyMethod("edit");
  store.setEditData(e);
};
const deletePe = (e) => {
  ElMessageBox.confirm("确定删除此条？", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
  }).then(async () => {
    try {
      let data = {};
      data.id = e.id;
      let res = await delPe(data);
      if (res.data.code == 200) {
        ElMessage.success("删除成功");
        getData();
      }
    } catch (e) {}
  });
};
const handleSizeChange = (e) => {
  get_pe_list(store.editData.category, store.editData.sub_category);
};
const handleCurrentChange = (e) => {
  get_pe_list(store.editData.category, store.editData.sub_category);
};
// const backUser = (e) => {
//   active.value = e.active;
// };
// const backHello = (e) => {
//   active.value = e.active;
// };
// const backMessage = (e) => {
//   active.value = e.active;
// };
// const backChat = (e) => {
//   active.value = e.active;
// };
// const backExperience = (e) => {
//   active.value = e.active;
// };
const backCreate = (e) => {
  get_pe_list(store.editData.category, store.editData.sub_category);
  active.value = e.active;
};

const get_pe_cate = async () => {
  try {
    let cate_res = await pe_cate();
    console.log(cate_res);
    if (cate_res.data.code == 200) {
      cates.value = cate_res.data.data;
    }
  } catch (e) {
    console.log(e);
  }
};

const get_pe_list = async (
  category = "article_info",
  sub_category = "summary"
) => {
  let data = {};
  data.page = currentPage.value;
  data.page_size = pageSize.value;
  data.category = category;
  data.sub_category = sub_category;
  try {
    let list_res = await peList(data);
    if (list_res.data.code == 1002) {
      tableData.value = list_res.data.data.items;
      total.value = list_res.data.data.total;
      tableData.value = tableData.value.map((item) => {
        item.createDateStr = transDate(item.created_at);
        item.updateDateStr = transDate(item.updated_at);
        item.statusText = item.status == 0 ? "未生效" : "线上生效中";
        return item;
      });
    }
    console.log(list_res);
  } catch (e) {
    console.log(e);
  }
};

const tabChange = (e) => {
  console.log(e);
};
const tabChange2 = (e) => {
  get_pe_list(activeName.value, childActiveName.value);
};
</script>

<style lang="scss" scoped>
.create-prompt {
  position: absolute;
  right: 20px;
  z-index: 999;
}
:deep(.el-tabs) {
  height: 100%;
}
:deep(.el-tabs__content) {
  height: 100%;
}
:deep(.el-tab-pane) {
  height: 100%;
}
</style>
